//
//  GXView.m
//  DJS
//
//  Created by mac on 2018/3/15.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GXView.h"
#import "CaseProgressViewController.h"

@implementation GXView

-(void)creatView{
    self.backgroundColor=[UIColor whiteColor];
    
    _scrollView=[UIScrollView new];
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAV_HEIGHT);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    _scrollView.scrollsToTop =NO;
    _scrollView.showsVerticalScrollIndicator = FALSE;
    _scrollView.showsHorizontalScrollIndicator = FALSE;
    _scrollView.bounces = NO;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.viewController.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UILabel *label=[UILabel new];
    [_scrollView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
    }];
    label.textColor=ColorWithRGB(49, 49, 49);
    label.font=[UIFont systemFontOfSize:16];
    NSString *st_name=[_dic objectForKey:@"st_name"];
    label.text=[NSString stringWithFormat:@"【%@>%@】",_str,st_name];
    
    UIView *beiJingView=[UIView new];
    [_scrollView addSubview:beiJingView];
    [beiJingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(_mainW-30);
        make.height.mas_equalTo(_mainH*0.3);
    }];
    beiJingView.layer.cornerRadius=4;
    beiJingView.backgroundColor=_LightGrey;
    
    _yiJianText=[UITextView new];
    [beiJingView addSubview:_yiJianText];
    [_yiJianText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_mainHeight*5);
        make.left.mas_equalTo(_mainWidth*10);
        make.right.mas_equalTo(-_mainWidth*10);
        make.bottom.mas_equalTo(-_mainHeight*5-25);
    }];
    _yiJianText.backgroundColor=_LightGrey;
    _yiJianText.text=@"请输入案件进度...";
    _yiJianText.font=[UIFont fontWithName:@"Helvetica" size:16];
    _yiJianText.textColor=ColorWithRGB(150, 150, 150);
    _yiJianText.delegate=self;
    
    _numLabel=[UILabel new];
    [beiJingView addSubview:_numLabel];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_yiJianText.mas_right).mas_equalTo(-15);
        make.bottom.mas_equalTo(-5);
    }];
    _numLabel.textColor=ColorWithRGB(149, 149, 149);
    _numLabel.font=[UIFont systemFontOfSize:14];
    _numLabel.text=@"0/200";
    
    
    UILabel *label2=[UILabel new];
    [_scrollView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(beiJingView.mas_bottom).mas_equalTo(15);
        make.left.mas_equalTo(label);
    }];
    label2.text=@"上传案件进度资料";
    label2.textColor=ColorWithRGB(49, 49, 49);
    label2.font=[UIFont systemFontOfSize:14];
    
    _quoteImagesView =[[LPDQuoteImagesView alloc] initWithFrame: CGRectMake(0, 80+_mainH*0.3, _mainW, _mainH*0.4) withCountPerRowInView: 3  cellMargin: 20 ];
    _quoteImagesView .maxSelectedCount=6;

    _quoteImagesView.collectionView .scrollEnabled = NO;

    [_scrollView addSubview: _quoteImagesView];
    
    UIButton *btn=[UIButton new];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(beiJingView);
        make.bottom.mas_equalTo(-20);
        make.height.mas_equalTo(45);
    }];
    [btn setTitle:@"提交" forState:0];
    btn.backgroundColor=_backgroundColor;
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.layer.cornerRadius=5;
    [btn addTarget:self action:@selector(tiJiao:) forControlEvents:UIControlEventTouchUpInside];
    
    _scrollView.contentSize = CGSizeMake(0,_mainH+100);
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"请输入案件进度..."]) {
        textView.text=@"";
    }
    textView.textColor=ColorWithRGB(50, 50, 50);
    
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        textView.text=@"请输入案件进度...";
    }
    textView.textColor=ColorWithRGB(150, 150, 150);
    
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    _numLabel.text=[NSString stringWithFormat:@"%ld/200",textView.text.length - range.length + text.length];
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        
        [_yiJianText resignFirstResponder];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    if ((textView.text.length - range.length + text.length) > 200)
    {
        NSString *substring = [text substringToIndex:200 - (textView.text.length - range.length)];
        NSMutableString *lastString = [textView.text mutableCopy];
        [lastString replaceCharactersInRange:range withString:substring];
        textView.text = [lastString copy];
        return NO;
    }
    else
    {
        return YES;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}
-(IBAction)tiJiao:(id)sender{
    
    
    NSArray<UIImage*> *imageArr=_quoteImagesView.selectedPhotos;
    NSString *schedule_content=_yiJianText.text;
    if ([schedule_content isEqualToString:@"请输入案件进度..."]) {
        schedule_content=@"";
    }
    if (([schedule_content isEqualToString:@""]||schedule_content==nil)&&imageArr==nil) {
        [ZQTools svpInfo:@"案件进度不嫩给空"];
        return;
    }
    if (imageArr.count==0&&([schedule_content isEqualToString:@""]||schedule_content==nil)) {
        [ZQTools svpInfo:@"你还没有选取图片"];
        return;
    }
    NSNumber *st_id1=[_dic objectForKey:@"st_id"];
    NSString *st_id=[NSString stringWithFormat:@"%@",st_id1];
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    NSString *token=[userDic objectForKey:@"token"];
    NSNumber *waiter_id1=[userDic objectForKey:@"waiter_id"];
    NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
    
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:_litigation_id,@"litigation_id",schedule_content,@"schedule_content",st_id,@"st_id",token,@"token",user_id,@"waiter_id",@"1",@"user_role", nil];
    [ZQTools afnPostImageWithDict:dic WithAddressUrl:@"waiter/Litigation/add_schedule" withImageArr:imageArr withFileName:@"schedule_img[]" fileOrImg:@"img" WithViewController:self.viewController success:^(id responseObject) {
        [ZQTools svpInfo:@"更新成功"];
        CaseProgressViewController   *jingDu=[[CaseProgressViewController alloc]init];
        jingDu.litigation_id=[NSNumber numberWithInteger:_litigation_id.integerValue];
        [ZQTools pushNextViewController:self.viewController andRootController:jingDu];
        
        
//        int index = (int)[[self.viewController.navigationController viewControllers]indexOfObject:self.viewController];
//        [self.viewController.navigationController popToViewController:[self.viewController.navigationController.viewControllers objectAtIndex:(index -3)] animated:YES];
//
//        NSNotification * notice = [NSNotification notificationWithName:@"gengxin" object:nil userInfo:dic];
//        //发送消息
//        [[NSNotificationCenter defaultCenter]postNotification:notice];
    }];
    
}


@end
